package entity

import (
	"fmt"
	"strconv"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// 同业业务
type CompTyywDr struct {
	ID    int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	NY    int       `gorm:"column:NY"`
	JYDS  string    `gorm:"column:JYDS"`
	YWLX  string    `gorm:"column:YWLX"`
	JYJE  float64   `gorm:"column:JYJE"`
	QXR   time.Time `gorm:"column:QXR"`
	DQR   time.Time `gorm:"column:DQR"`
	TS    float64   `gorm:"column:TS"`
	LL    float64   `gorm:"column:LL"`
	FTPLL float64   `gorm:"column:FTPLL"`
	LC    float64   `gorm:"column:LC"`
	TS2   float64   `gorm:"column:TS2"`
	MNLR  float64   `gorm:"column:MNLR"`
	NRJ   float64   `gorm:"column:NRJ"`
	KHJL1 string    `gorm:"column:KHJL1"`
	ZB1   float64   `gorm:"column:ZB1"`
	KHJL2 string    `gorm:"column:KHJL2"`
	ZB2   float64   `gorm:"column:ZB2"`
	KHJL3 string    `gorm:"column:KHJL3"`
	ZB3   float64   `gorm:"column:ZB3"`
	KHJL4 string    `gorm:"column:KHJL4"`
	ZB4   float64   `gorm:"column:ZB4"`
	KHJL5 string    `gorm:"column:KHJL5"`
	ZB5   float64   `gorm:"column:ZB5"`
	CUID  int       `gorm:"column:CUID"`
	EMID1 int       `gorm:"column:EMID1"`
	EMID2 int       `gorm:"column:EMID2"`
	EMID3 int       `gorm:"column:EMID3"`
	EMID4 int       `gorm:"column:EMID4"`
	EMID5 int       `gorm:"column:EMID5"`
	DTID  int       `gorm:"column:DTID"`
	DRJG  string    `gorm:"column:DRJG"`
}

func (CompTyywDr) TableName() string {
	return "T_COMP_TYYW_DR"
}

// BeforeCreate
func (i *CompTyywDr) BeforeCreate(tx *gorm.DB) (err error) {
	count := 0
	_ = base.CHDB.Raw("select count(1) from T_COMP_CLIE where CUNM=?", i.JYDS).Row().Scan(&count)
	if count > 0 {
		_ = base.CHDB.Raw("select CUID from T_COMP_CLIE where CUNM=?", i.JYDS).Row().Scan(&i.CUID)
		i.DRJG = "导入成功！"
	}

	if i.CUID == 0 && i.KHJL1 == "" {
		i.EMID1 = 0
		i.ZB1 = 100
		i.KHJL1 = "G36000"
		i.DRJG = "客户在系统中不存在，且未指定客户经理，将自动分配到分行公共！"
	}

	if i.KHJL1 != "" {
		sum := i.ZB1 + i.ZB2 + i.ZB3 + i.ZB4 + i.ZB5
		if sum != 100 {
			return fmt.Errorf("占比之和必须为100！")
		}
		var emid1, emid2, emid3, emid4, emid5 int
		var emnm1, emnm2, emnm3, emnm4, emnm5 string

		count := 0
		sqlExYg := "select count(1) from T_SysUser where SysUserName=?"
		_ = tx.Raw(sqlExYg, i.KHJL1).Row().Scan(&count)
		if count > 1 {
			return fmt.Errorf("员工1:%s在系统中存在多个名字，请填入工号！", i.KHJL1)
		}

		sqlEx := "select ID,SysUserName from T_SysUser where SysUserID=? or SysUserName=?"
		row := tx.Raw(sqlEx, i.KHJL1, i.KHJL1).Row()
		row.Scan(&emid1, &emnm1)
		if emid1 == 0 && emnm1 != "分行公共" {
			return fmt.Errorf("员工号1:%s在系统中没有找到！", i.KHJL1)
		}
		i.EMID1 = emid1
		if i.ZB2 > 0 {
			_ = tx.Raw(sqlExYg, i.KHJL2).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工2:%s在系统中存在多个名字，请填入工号！", i.KHJL2)
			}
			row = tx.Raw(sqlEx, i.KHJL2, i.KHJL2).Row()
			row.Scan(&emid2, &emnm2)
			if emid2 == 0 && emnm2 != "分行公共" {
				return fmt.Errorf("员工号2:%s在系统中没有找到！", i.KHJL2)
			}
			i.EMID2 = emid2
		}
		if i.ZB3 > 0 {
			_ = tx.Raw(sqlExYg, i.KHJL3).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工3:%s在系统中存在多个名字，请填入工号！", i.KHJL3)
			}
			row = tx.Raw(sqlEx, i.KHJL3, i.KHJL3).Row()
			row.Scan(&emid3, &emnm3)
			if emid3 == 0 && emnm3 != "分行公共" {
				return fmt.Errorf("员工号3:%s在系统中没有找到！", i.KHJL2)
			}
			i.EMID3 = emid3
		}
		if i.ZB4 > 0 {
			_ = tx.Raw(sqlExYg, i.KHJL4).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工4:%s在系统中存在多个名字，请填入工号！", i.KHJL4)
			}
			row = tx.Raw(sqlEx, i.KHJL4, i.KHJL4).Row()
			row.Scan(&emid4, &emnm4)
			if emid4 == 0 && emnm4 != "分行公共" {
				return fmt.Errorf("员工号4:%s在系统中没有找到！", i.KHJL4)
			}
			i.EMID4 = emid4
		}
		if i.ZB5 > 0 {
			_ = tx.Raw(sqlExYg, i.KHJL5).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工5:%s在系统中存在多个名字，请填入工号！", i.KHJL5)
			}
			row = tx.Raw(sqlEx, i.KHJL5, i.KHJL5).Row()
			row.Scan(&emid5, &emnm5)
			if emid5 == 0 && emnm5 != "分行公共" {
				return fmt.Errorf("员工号5:%s在系统中没有找到！", i.KHJL5)
			}
			i.EMID5 = emid5
		}
	}

	if i.DRJG == "" {
		i.DRJG = "导入成功！"
	}

	dtid := strconv.Itoa(i.NY)
	dtid = dtid[:4] + "-" + dtid[4:6] + "-01"
	err = tx.Raw("SELECT REPLACE (LAST_DAY(STR_TO_DATE(?, '%Y-%m-%d')),'-','')as dtid;", dtid).Row().Scan(&i.DTID)

	return
}

// BaseParaEF 一个结构
func CompTyywDrEF() base.EntityFunc {
	return func() interface{} {
		entity := CompTyywDr{}
		return &entity
	}
}

// BaseParaESF 数组结构体
func CompTyywDrESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompTyywDr
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("CompTyywDr", base.NewDefaultDao(CompTyywDrEF(), CompTyywDrESF(), nil, "my"))
}
